<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <title>计算驾车时间与距离</title>
    <script src="//api.map.baidu.com/api?type=webgl&v=1.0&ak=jYaBAHQejLwjG5LCM88MZQgv7N4oBGPZ"
            type="text/javascript"></script>
    <script src="/jquery-3.6.0.min.js"></script>
    <style>
        html, body {
            width: 100%;
            height: 100%;
            padding: 0;
            margin: 0;
        }

        #allmap {
            width: 100%;
            height: 100%;
            overflow: hidden;
        }
    </style>
</head>
<body>
<div id='allmap'></div>


<script>
    var map = new BMapGL.Map("allmap");
    map.centerAndZoom(new BMapGL.Point(116.404, 39.915), 12);

    var searchComplete = function (results) {
        if (transit.getStatus() != BMAP_STATUS_SUCCESS) {
            return;
        }
        var plan = results.getPlan(0);
        console.log(JSON.stringify(results._start)
            + "|||" + JSON.stringify(results._end)
            + "|||" + plan.getDistance(true)
            + "||||" + plan.getDuration(true));
        $.ajax({
            url: '/pipeline/save',
            type: 'POST',
            data: {
                "start": results._start.point,
                "end": results._end.point,
                "distance": plan.getDistance(false)
            },
            success: function (response) {
                console.log(response.result);
                //刷新页面
            },
            error: function (xhr, status, error) {
                // 请求失败的处理逻辑
                console.log(error);
            }
        });

        console.log(JSON.stringify(results._start)
            + "|||" + JSON.stringify(results._end)
            + "|||" + plan.getDistance(true)
            + "||||" + plan.getDuration(true));
    }
    var transit = new BMapGL.DrivingRoute(map, {
        renderOptions: {map: map},
        onSearchComplete: searchComplete,
        onPolylinesSet: function () {
            setTimeout(function () {


            }, "1000");
        }
    });


    var formatPoint = function (srcPoint) {
        return new BMapGL.Point(srcPoint.lng, srcPoint.lat)
    }


    $(document).ready(function () {
        $.ajax({
            url: '/pipeline/find',
            type: 'POST',
            data: {},
            success: function (response) {

                console.log(response.result);
                for (let i = 0; i < response.result.length; i++) {
                    console.log(response.result[i].apoint);
                    var item = JSON.parse(response.result[i].apoint)
                    var item2 = JSON.parse(response.result[i].bpoint)
                    console.log(item);
                    transit.search(formatPoint(item), formatPoint(item2));
                }

            },
            error: function (xhr, status, error) {
                // 请求失败的处理逻辑
                console.log(error);
            }
        });
    });
</script>
</body>
</html>
